<?php

namespace App\Http\Controllers\Manage;

use App\Helpers\ResponseData;
use App\Helpers\Untils;
use App\Http\Controllers\Controller;
use App\Models\CustomerComplaint;
use App\Models\Department;
use App\Models\ShopKeeperRecord;
use App\Models\SystemUser;
use App\Models\TrainingMaterial;
use App\Models\TrainMaterialRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//培训资料
class TrainMaterialController extends Controller
{
    use ResponseData,Untils;
    public function list(TrainingMaterial $model,Request $request){
        $size = $request->input('size',10);
        if($request->filled('shop_id')){
            $model = $model->where('shop_id',$request->shop_id);
        }
        if($request->filled('start')&&$request->filled('end')){
            $model = $model->whereBetween('created_at',[$request->start,$request->end]);
        }
        if($request->filled('cutomer_name')){
            $model = $model->where('contact_name','like','%'.$request->cutomer_name.'%');
        }
        if($request->filled('system_user')){
            $model = $model->where('system_user_name','like','%'.$request->system_user.'%');
        }
        if($request->filled('status')&&$request->status=='all'){
            $data = $model->orderBy('id','desc')->get();
        }else{
            $data = $model->orderBy('id','desc')->paginate($size);
        }
        return $this->success($data);
    }
    public function store(TrainingMaterial $model,Request $request){
        $user = $request->user();
        $data = $request->all();
        $model->fill($data);
        $model->save();
        $this->setUnreadMessage($request->shop_id,$user);
        return $this->success('保存成功');
    }
    public function edit(TrainingMaterial $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
    public function update(TrainingMaterial $model,Request $request){
        $user = $request->user();
        $data = $model->find($request->id);
        $data->update($request->all());
        $this->setUnreadMessage($request->id,$user);
        return $this->success('修改成功');
    }
    public function destory(TrainingMaterial $model,Request $request){
        $data = $model->find($request->id);
        $data->delete();
    }
    //获取是否有未读消息
    public function checkUpdate(TrainMaterialRecord $model,Request $request){
        $user = $request->user();
        $data = $model->where('shop_manage_id',$request->shop_manage_id)
            ->where('user_id',$user->id)
            ->where('is_read',1)
            ->first();
        if($data){
            return $this->success(2);
        }else{
            return $this->success(1);
        }
    }
    //设置未读变成已读消息
    public function setRead(TrainMaterialRecord $model,Request $request){
        $user = $request->user();
        $model->where('shop_manage_id',$request->shop_manage_id)
            ->where('user_id',$user->id)
            ->update(['is_read'=>2]);
        return $this->success('更新成功');
    }
    //设置未读信息
    protected function setUnreadMessage($shop_id,$user){
        $users = $this->getUnreadUser($shop_id);
        if($users){
            $map = [];
            foreach ($users as $k=>$v){
                $map[$k]['shop_manage_id'] = $shop_id;
                $map[$k]['user_id'] = $v;
                $map[$k]['operator_name'] = $user->name;
                $map[$k]['operator_department'] = $this->getDepartment($user->department_id);
                $map[$k]['operator_time'] = time();
            }
            DB::table('train_material_records')->insert($map);
        }

    }
    //获取部门相关信息
    protected function getDepartment($id){
        if($id>0){
            return Department::query()->where('id',$id)->value('name');
        }
        return '';
    }
}
